package com.example.knowman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.knowman.ObService.OBknowLabelDetailService;
import com.example.knowman.ObService.OBlabelCategoryService;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.ReturnMessage.MessageBackKnowLabelDetailId;
import com.example.knowman.dao.knowLabelDetailEx;
import com.example.knowman.dao.knowMainEx;
import com.example.knowman.dao.labelCategoryWithStaffNAndKnowCategoryN;
import com.example.knowman.entity.knowLabelDetail;
import com.example.knowman.entity.knowMain;
import com.example.knowman.entity.labelCategory;
import com.example.knowman.mapper.KnowLabelDetailMapper;
import com.example.knowman.mapper.LabelCategoryMapper;
import com.example.knowman.mapper.StaffListMapper;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.IStaffListService;
import com.example.knowman.service.IknowLabelDetailService;
import com.example.knowman.service.IlabelCategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class KnowLabelDetailServiceImpl extends ServiceImpl<KnowLabelDetailMapper, knowLabelDetail> implements IknowLabelDetailService {

    private final LabelCategoryMapper labelCategoryMapper;

    private final OBlabelCategoryService oblabelCategoryService;


    public KnowLabelDetailServiceImpl(LabelCategoryMapper labelCategoryMapper,OBlabelCategoryService oblabelCategoryService) {

        this.labelCategoryMapper = labelCategoryMapper;
        this.oblabelCategoryService = oblabelCategoryService;

    }


    @Override
    public IPage<knowLabelDetailEx> getDetails(Integer pageNum, Integer pageSize, Integer knowLblId) {
        Map<Integer,labelCategory> LabelList = oblabelCategoryService.getLabelDetails();

        IPage<knowLabelDetailEx> Respage = new Page<>(pageNum, pageSize);
        List<knowLabelDetailEx> recordsL = new ArrayList<>();

        IPage<knowLabelDetail> page = new Page<>(pageNum, pageSize);
        page = this.lambdaQuery().select()
                .eq(knowLblId !=null ,knowLabelDetail::getKnowLblId,knowLblId)
                .page(page);

//        System.out.println(page.getRecords());
//        System.out.println(LabelList);
        Respage.setTotal(page.getTotal());
        for (knowLabelDetail item:page.getRecords()){
            knowLabelDetailEx temp = new knowLabelDetailEx();

            temp.setKnowLblDtlId(item.getKnowLblDtlId());
            temp.setKnowLblId(knowLblId);
            temp.setLblContent(item.getLblContent());
            temp.setLabelCateId(item.getLabelCateId());
            labelCategory lt = LabelList.get(item.getLabelCateId());
            temp.setLabelColor(lt.getLabelColor());
            temp.setLabelName(lt.getLabelName());
            temp.setCreatetime(item.getCreatetime());

            recordsL.add(temp);
        }

        Respage.setRecords(recordsL);

        return Respage;
    }

    @Override
    public MessageBackKnowLabelDetailId Insert(Integer knowLblId, String lblContent, Integer labelCateId) {
        MessageBackKnowLabelDetailId message = new MessageBackKnowLabelDetailId();

        knowLabelDetail temp = new knowLabelDetail();
        temp.setKnowLblId(knowLblId);
        temp.setLblContent(lblContent);
        temp.setLabelCateId(labelCateId);

        try{
            this.save(temp);
            List<knowLabelDetail> insertedRecord = this.lambdaQuery().select()
                    .eq(knowLblId !=null,knowLabelDetail::getKnowLblId,knowLblId)
                    .eq(lblContent !=null && !lblContent.isEmpty(),knowLabelDetail::getLblContent,lblContent)
                    .eq(labelCateId !=null , knowLabelDetail::getLabelCateId,labelCateId)
                    .orderByDesc(knowLabelDetail::getCreatetime).list();
            message.setCode(200);
            message.setTip("新增成功");
            message.setKnowLblDtlId(insertedRecord.get(0).getKnowLblDtlId());
        }catch (Exception e){
            message.setCode(100);
            message.setTip("新增失败");
        }

        return message;
    }

    @Override
    public Message deleteByknowLblDtlId(Integer knowLblDtlId) {
        Message message = new Message();
        QueryWrapper<knowLabelDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("\"knowLblDtlId\"",knowLblDtlId);
        try{
            this.remove(wrapper);
            message.setCode(200);
            message.setTip("删除成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("删除失败");
        }

        return message;
    }
}
